//
//  ShipingListDetailViewController.m
//  B段项目
//
//  Created by 徐茂怀 on 15/11/3.
//  Copyright © 2015年 徐茂怀. All rights reserved.
//

#import "ShipingListDetailViewController.h"
#define POSTURL @"http://api.haodou.com/index.php?appid=2&appkey=9ef269eec4f7a9d07c73952d06b5413f&format=json&sessionid=1446530023772&vc=77&vn=5.4.0&loguid=0&deviceid=haodou99000505722929&uuid=a6afb132369a979ba3427eec69189063&channel=xiaomi_v540&method=Info.getVideoList&virtual=&signmethod=md5&v=2&timestamp=1446552993&nonce=0.266431913636454&appsign=2aa630c5098bd7839eed4ba027100179"
#import "FitHeader.h"
#import "SingleTon.h"
#import "ShipingPlayViewController.h"
static NSInteger offset = 0;
static NSInteger flag = 0;
@interface ShipingListDetailViewController ()<UITableViewDataSource,UITableViewDelegate,ChooseList>
@property(nonatomic, strong)NSMutableArray *listArray;
@property(nonatomic, strong)NSMutableArray *cateArray;
@end

@implementation ShipingListDetailViewController

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = NO;
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = YES;
}

-(void)loadView
{
    [super loadView];
    _titleView = [[TitleView alloc]initWithFrame:CGRectMake(50 * FitWidth, 0 , 300 * FitWidth, 64 * FitHeight)];
    _titleView.titleLabel.text = self.cateName;
    self.navigationItem.titleView = _titleView;
    
    _titleView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showList)];
    [_titleView addGestureRecognizer:tap];
}

-(void)showList
{
    
    if (flag == 0) {
        [UIView animateWithDuration:.2 animations:^{
            
            _titleView.xialaImageView.transform = CGAffineTransformMakeRotation(M_PI);
        }];

        _listView = [[TitleListView alloc] initWithFrame:CGRectMake(30 * FitWidth, 0, 230  * FitWidth, 500 * FitHeight) cateArray:_cateArray];
        _listView.alpha = .93;

        _listView.delegate = self;
        [self.view addSubview:_listView];
        flag = 1;

    }
        else
        {
            [UIView animateWithDuration:.2 animations:^{
                
                _titleView.xialaImageView.transform = CGAffineTransformMakeRotation( M_PI * 2);
            }];
            [_listView removeFromSuperview];
            flag = 0;
        }
    
}

-(void)chooceList:(NSInteger)CateID TitleName:(NSString *)titleName
{
    [_listArray removeAllObjects];
     flag = 0;
    offset = 0;
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"加载中,不要着急";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.75 green:0.77 blue:0.77 alpha:0.5];
    [self.view addSubview:_progressView];
    _cate_id = CateID;
    self.titleView.titleLabel.text = titleName;
    [self getData];
    [_tableView reloadData];
    [_listView removeFromSuperview];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    _listArray = [NSMutableArray array];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 )];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView setTableFooterView:v];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backView)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
    
    [self.view addSubview:_tableView];
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"加载中,不要着急";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.75 green:0.77 blue:0.77 alpha:0.5];
    [self.view addSubview:_progressView];
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_listArray removeAllObjects];
        offset = 0;
        [self getData];
    }];
    
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        offset += 20;
        [self getData];
    }];
    [self getData];
}

-(void)backView
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)getData
{
    NSString *s = [NSString stringWithFormat:@"%ld",(long)_cate_id];
    NSString *off = [NSString stringWithFormat:@"%ld",(long)offset];
    [XMHNetWorkongMethods getDataVyString:POSTURL BodyString:[NSDictionary dictionaryWithObjectsAndKeys:@"20",@"limit",off,@"offset",s,@"cate_id", nil] WithDataBlock:^(id data) {
        NSDictionary *dic = [data objectForKey:@"result"];
        _cateArray = [dic objectForKey:@"cate"];
        NSArray *array = [dic objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            [_listArray addObject:dic];
        }
        if (!array.count) {
            [_tableView.footer endRefreshingWithNoMoreData];
        }
        else
        {
            [_tableView.footer endRefreshing];
            [_tableView.header endRefreshing];
            [_progressView removeFromSuperview];
            [self.tableView reloadData];
        }

    } WithFailure:^(NSError *error) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"网络故障" message:@"请检查网络" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            [alertController removeFromParentViewController];
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshing];
            [_progressView removeFromSuperview];
        }];
        UIAlertAction *going = [UIAlertAction actionWithTitle:@"继续加载" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self getData];
            [alertController removeFromParentViewController];
        }];
        [alertController addAction:going];
        [self presentViewController:alertController animated:YES completion:nil];
        [alertController addAction:ok];

    }];
   
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [_listView removeFromSuperview];
    flag = 0;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _listArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * FitWidth;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    ShipingListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ShipingListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    ShipingModel *model = [[ShipingModel alloc]init];
    [model setValuesForKeysWithDictionary:_listArray[indexPath.row]];
    cell.model = model;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.hidesBottomBarWhenPushed = YES;
    [[SingleTon shareSingleTon]setRid:[_listArray[indexPath.row]objectForKey:@"RecipeId"]];
    ShipingPlayViewController *shipingvc = [[ShipingPlayViewController alloc]init];
    [self.navigationController pushViewController:shipingvc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
